package com.runchamp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;

/* Activity to configure app settings */
public class UserSettings extends BaseActivity implements GoogleApiClient.ConnectionCallbacks {

    private SharedPreferences prefs;
    private double distanceFreq;
    private double timeFreq;
    private int informationIndex;
    private boolean onTOuchAlerts;
    private CheckBox onTouchBox;
    private Spinner spinner1, spinner2, spinner3;
    private SharedPreferences.Editor editor;
    private Button signoutButton;
    private GoogleApiClient mGoogleApiClient;
    private LinearLayout timeLay, distLay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLayoutInflater().inflate(R.layout.activity_user_settings, frameLayout);
        mDrawerList.setItemChecked(position, true);
        setTitle(listArray.get(position));
        position = 2;
        prefs = getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
        distanceFreq = Double.valueOf(prefs.getString("Settings/distanceFreq", "100"));
        timeFreq = Double.valueOf(prefs.getString("Settings/timeFreq", "100"));
        informationIndex = Integer.valueOf(prefs.getString("Settings/informationIndex", "100"));
        onTOuchAlerts = Integer.valueOf(prefs.getString("Settings/onTouchAlerts", "100")) == 1 ? true : false;
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        timeLay = (LinearLayout) findViewById(R.id.time_layout);
        distLay = (LinearLayout) findViewById(R.id.dist_layout);
        onTouchBox = (CheckBox) findViewById(R.id.onTouchBox);
        if (onTOuchAlerts) {
            onTouchBox.setChecked(true);
        }

        prefs = getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
        editor = prefs.edit();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();

        /*
        * initialize the distance spinner according to user settings */
        if (distanceFreq == -1) {
            spinner3.setSelection(0);
        } else if (distanceFreq == 0.1) {
            spinner3.setSelection(1);
        } else if (distanceFreq == 0.2) {
            spinner3.setSelection(2);
        } else if (distanceFreq == 0.5) {
            spinner3.setSelection(3);
        } else if (distanceFreq == 1) {
            spinner3.setSelection(4);
        } else if (distanceFreq == 2) {
            spinner3.setSelection(5);
        } else if (distanceFreq == 5) {
            spinner3.setSelection(6);
        } else if (distanceFreq == 10) {
            spinner3.setSelection(7);
        }

        /*
        * initialize the time spinner according to user settings */
        if (timeFreq == -1) {
            spinner2.setSelection(0);
        } else if (timeFreq == 10) {
            spinner2.setSelection(1);
        } else if (timeFreq == 30) {
            spinner2.setSelection(2);
        } else if (timeFreq == 60) {
            spinner2.setSelection(3);
        } else if (timeFreq == 120) {
            spinner2.setSelection(4);
        } else if (timeFreq == 300) {
            spinner2.setSelection(5);
        } else if (timeFreq == 600) {
            spinner2.setSelection(6);
        } else if (timeFreq == 1200) {
            spinner2.setSelection(7);
        }


        /*
        * initialize the information index spinner according to user settings */
        if (informationIndex == -1) {
            spinner1.setSelection(0);
        } else if (informationIndex == 1) {
            spinner1.setSelection(1);
        } else if (informationIndex == 2) {
            spinner1.setSelection(2);
        }

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    informationIndex = -1;
                    timeLay.setVisibility(View.INVISIBLE);
                    distLay.setVisibility(View.INVISIBLE);
                }
                if (position == 1) {
                    timeLay.setVisibility(View.VISIBLE);
                    distLay.setVisibility(View.VISIBLE);
                    informationIndex = 1;
                }
                if (position == 2) {
                    timeLay.setVisibility(View.VISIBLE);
                    distLay.setVisibility(View.VISIBLE);
                    informationIndex = 2;
                }

                editor.putString("Settings/informationIndex", String.valueOf(informationIndex));
                editor.commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        timeFreq = -1;
                        break;
                    case 1:
                        timeFreq = 10;
                        break;
                    case 2:
                        timeFreq = 30;
                        break;
                    case 3:
                        timeFreq = 60;
                        break;
                    case 4:
                        timeFreq = 120;
                        break;
                    case 5:
                        timeFreq = 300;
                        break;
                    case 6:
                        timeFreq = 600;
                        break;
                    case 7:
                        timeFreq = 1200;
                        break;
                }
                editor.putString("Settings/timeFreq", String.valueOf(timeFreq));
                editor.commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        spinner3 = (Spinner) findViewById(R.id.spinner3);
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        distanceFreq = -1;
                        break;
                    case 1:
                        distanceFreq = 0.1;
                        break;
                    case 2:
                        distanceFreq = 0.2;
                        break;
                    case 3:
                        distanceFreq = 0.5;
                        break;
                    case 4:
                        distanceFreq = 1;
                        break;
                    case 5:
                        distanceFreq = 2;
                        break;
                    case 6:
                        distanceFreq = 5;
                        break;
                    case 7:
                        distanceFreq = 10;
                        break;
                }
                editor.putString("Settings/distanceFreq", String.valueOf(distanceFreq));
                editor.commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        signoutButton = (Button) findViewById(R.id.plus_disconnect_button);
        signoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mGoogleApiClient.connect();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_user_settings, menu);
        return true;
    }

    public void onBoxClick(View v) {
        int checked = onTouchBox.isChecked() == true ? 1 : 0;
        editor.putString("Settings/onTouchAlerts", String.valueOf(checked));
        editor.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConnected(Bundle bundle) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        prefs = getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.remove("userID");
        editor.commit();
        startActivity(new Intent(this, Login.class));
    }

    @Override
    public void onConnectionSuspended(int i) {

    }
}
